;/**************************************************************************//**
; * @file     startup_ARMCM4.s
; * @brief    CMSIS Core Device Startup File for
; *           ARMCM4 Device Series
; * @version  V1.08
; * @date     23. November 2012
; *
; * @note
; *
; ******************************************************************************/
;/* Copyright (c) 2011 - 2012 ARM LIMITED
;
;   All rights reserved.
;   Redistribution and use in source and binary forms, with or without
;   modification, are permitted provided that the following conditions are met:
;   - Redistributions of source code must retain the above copyright
;     notice, this list of conditions and the following disclaimer.
;   - Redistributions in binary form must reproduce the above copyright
;     notice, this list of conditions and the following disclaimer in the
;     documentation and/or other materials provided with the distribution.
;   - Neither the name of ARM nor the names of its contributors may be used
;     to endorse or promote products derived from this software without
;     specific prior written permission.
;   *
;   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;   ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
;   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;   POSSIBILITY OF SUCH DAMAGE.
;   ---------------------------------------------------------------------------*/
;/*
;//-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
;*/


; <h> Stack Configuration
;   <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>

Stack_Size      EQU     0x00001000

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp


; <h> Heap Configuration
;   <o>  Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>

Heap_Size       EQU     0x00001000

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit


                PRESERVE8
                THUMB


; Vector Table Mapped to Address 0 at Reset

                AREA    RESET, DATA, READONLY
                EXPORT  __Vectors
                EXPORT  __Vectors_End
                EXPORT  __Vectors_Size

__Vectors       DCD     __initial_sp              ; Top of Stack
                DCD     Reset_Handler             ; Reset Handler
                DCD     NMI_Handler               ; NMI Handler
                DCD     HardFault_Handler         ; Hard Fault Handler
                DCD     MemManage_Handler         ; MPU Fault Handler
                DCD     BusFault_Handler          ; Bus Fault Handler
                DCD     UsageFault_Handler        ; Usage Fault Handler
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     SVC_Handler               ; SVCall Handler
                DCD     DebugMon_Handler          ; Debug Monitor Handler
                DCD     0                         ; Reserved
                DCD     PendSV_Handler            ; PendSV Handler
                DCD     SysTick_Handler           ; SysTick Handler

                ; External Interrupts
                DCD     OTP_IRQHandler            ;  0:  OTP
                DCD     PMU_IRQHandler            ;  1:  PMU
                DCD     TC_IRQHandler             ;  2:  TC
                DCD     PIT1_IRQHandler           ;  3:  PIT1
                DCD     PIT2_IRQHandler           ;  4:  PIT2
                DCD     EDMAC1_IRQHandler         ;  5:  EDMAC1
                DCD     0                         ;  6:  Reserved
                DCD     DMA1_IRQHandler           ;  7:  DMA1
                DCD     DMA2_IRQHandler           ;  8:  DMA2
                DCD     0                         ;  9:  Reserved
                DCD     TRNG_IRQHandler           ;  10: Reserved
                DCD     0                         ;  11: Reserved
                DCD     0                         ;  12: Reserved
                DCD     0                         ;  13: Reserved
                DCD     SEC_PGD_LD_FD_IRQHandler  ;  14: SEC_DET/PGD/LD/FD Combined Interrupt
                DCD     0                         ;  15: Reserved
                DCD     0                         ;  16: Reserved
                DCD     0                         ;  17: Reserved
                DCD     0                         ;  18: Reserved
                DCD     AES_IRQHandler            ;  19: SMS4/AES
                DCD     QADC_IRQHandler           ;  20: QADC                
                DCD     0                         ;  21: Reserved
                DCD     0                         ;  22: Reserved
                DCD     0                         ;  23: Reserved
                DCD     0                         ;  24: Reserved
                DCD     SPI2_IRQHandler           ;  25: SPI2
                DCD     SPI1_IRQHandler           ;  26: SPI1
                DCD     0                         ;  27: Reserved
                DCD     SPI3_IRQHandler           ;  28: SPI3
                DCD     SPIM1_IRQHandler          ;  29: SPIM1
                DCD     0                         ;  30: Reserved
                DCD     0                         ;  31: Reserved
                DCD     SCI1_IRQHandler           ;  32: SCI1
                DCD     SCI2_IRQHandler           ;  33: SCI2
                DCD     0                         ;  34: Reserved
                DCD     0                         ;  35: Reserved
                DCD     I2C1_IRQHandler           ;  36: I2C1
                DCD     PWM0_IRQHandler           ;  37: PWM0
                DCD     PWM1_IRQHandler           ;  38: PWM1
                DCD     PWM2_IRQHandler           ;  39: PWM2
                DCD     PWM3_IRQHandler           ;  40: PWM3
                DCD     EPORT0_0_IRQHandler	      ;  41: EPORT0_0
                DCD     EPORT0_1_IRQHandler	      ;  42: EPORT0_1
                DCD     EPORT0_2_IRQHandler       ;  43: EPORT0_2
                DCD     EPORT0_3_IRQHandler       ;  44: EPORT0_3
                DCD     EPORT0_4_IRQHandler       ;  45: EPORT0_4
                DCD     EPORT0_5_IRQHandler       ;  46: EPORT0_5
                DCD     EPORT0_6_IRQHandler       ;  47: EPORT0_6
                DCD     EPORT0_7_IRQHandler       ;  48: EPORT0_7
                DCD     EPORT1_0_IRQHandler	      ;  49: EPORT1_0
                DCD     EPORT1_1_IRQHandler	      ;  50: EPORT1_1
                DCD     EPORT1_2_IRQHandler       ;  51: EPORT1_2
                DCD     EPORT1_3_IRQHandler       ;  52: EPORT1_3
                DCD     EPORT1_4_IRQHandler       ;  53: EPORT1_4
                DCD     EPORT1_5_IRQHandler       ;  54: EPORT1_5
                DCD     EPORT1_6_IRQHandler       ;  55: EPORT1_6
                DCD     EPORT1_7_IRQHandler	      ;  56: EPORT1_7
                DCD     0                         ;  57: Reserved
                DCD     0                         ;  58: Reserved	
                DCD     0                         ;  59: Reserved               
                DCD     0                         ;  60: Reserved							
                DCD     SCI3_IRQHandler           ;  61: SCI3				
                DCD     0                         ;  62: Reserved				
                DCD     0                         ;  63: Reserved				
                DCD     0                         ;  64: Reserved
                DCD     0                         ;  65: Reserved
                DCD     0                         ;  66: Reserved
                DCD     0                         ;  67: Reserved
                DCD     0                         ;  68: Reserved
                DCD     0                         ;  69: Reserved				
                DCD     0                         ;  70: Reserved
                DCD     0                         ;  71: Reserved
                DCD     0                         ;  72: Reserved
                DCD     0                         ;  73: Reserved
                DCD     0                         ;  74: Reserved
                DCD     0                         ;  75: Reserved
                DCD     0                         ;  76: Reserved
                DCD     0                         ;  77: Reserved				
                DCD     0                         ;  78: Reserved
                DCD     0                         ;  79: Reserved				
                DCD     0                         ;  80: Reserved
                DCD     0                         ;  81: Reserved
                DCD     0                         ;  82: Reserved
                DCD     0                         ;  83: Reserved
                DCD     PWMT1_IRQHandler          ;  84: PWMT1				
                DCD     PWMT2_IRQHandler          ;  85: PWMT2
                DCD     0                         ;  86: Reserved
                DCD     PWM4_IRQHandler           ;  87: PWM4
                DCD     PWM5_IRQHandler           ;  88: PWM5
                DCD     PWM6_IRQHandler           ;  89: PWM6
                DCD     PWM7_IRQHandler           ;  90: PWM7
                DCD     0                         ;  91: Reserved
                DCD     EPORT2_0_IRQHandler	      ;  92: EPORT2_0
                DCD     EPORT2_1_IRQHandler	      ;  93: EPORT2_1				
                DCD     EPORT2_2_IRQHandler	      ;  94: EPORT2_2
                DCD     EPORT2_3_IRQHandler	      ;  95: EPORT2_3
                DCD     EPORT2_4_IRQHandler	      ;  96: EPORT2_4
                DCD     EPORT2_5_IRQHandler	      ;  97: EPORT2_5
                DCD     EPORT2_6_IRQHandler	      ;  98: EPORT2_6
                DCD     EPORT2_7_IRQHandler	      ;  99: EPORT2_7
                DCD     0	                      ;  100: Reserved
                DCD     0	                      ;  101: Reserved				
                DCD     0               	      ;  102: Reserved
                DCD     0               	      ;  103: Reserved
                DCD     0               	      ;  104: Reserved
                DCD     0               	      ;  105: Reserved
                DCD     0               	      ;  106: Reserved
                DCD     0                         ;  107: Reserved				
                DCD     0               	      ;  108: Reserved
                DCD     0               	      ;  109: Reserved				
                DCD     0               	      ;  110: Reserved
                DCD     0               	      ;  111: Reserved
                DCD     0               	      ;  112: Reserved
                DCD     0               	      ;  113: Reserved
                DCD     0               	      ;  114: Reserved
                DCD     0               	      ;  115: Reserved
                DCD     EPORT5_0_IRQHandler	      ;  116: EPORT5_0
                DCD     EPORT5_1_IRQHandler	      ;  117: EPORT5_1				
                DCD     EPORT5_2_IRQHandler	      ;  118: EPORT5_2
                DCD     EPORT5_3_IRQHandler	      ;  119: EPORT5_3
                DCD     EPORT5_4_IRQHandler	      ;  120: EPORT5_4
                DCD     EPORT5_5_IRQHandler	      ;  121: EPORT5_5
                DCD     EPORT5_6_IRQHandler	      ;  122: EPORT5_6
                DCD     EPORT5_7_IRQHandler	      ;  123: EPORT5_7				
                DCD     EPORT6_0_IRQHandler	      ;  124: EPORT6_0
                DCD     EPORT6_1_IRQHandler	      ;  125: EPORT6_1				
                DCD     EPORT6_2_IRQHandler	      ;  126: EPORT6_2
                DCD     EPORT6_3_IRQHandler	      ;  127: EPORT6_3
                DCD     EPORT6_4_IRQHandler	      ;  128: EPORT6_4
                DCD     EPORT6_5_IRQHandler	      ;  129: EPORT6_5
                DCD     EPORT6_6_IRQHandler	      ;  130: EPORT6_6
                DCD     EPORT6_7_IRQHandler	      ;  131: EPORT6_7
                DCD     EPORT7_0_IRQHandler	      ;  132: EPORT7_0
                DCD     EPORT7_1_IRQHandler	      ;  133: EPORT7_1				
                DCD     EPORT7_2_IRQHandler	      ;  134: EPORT7_2
                DCD     EPORT7_3_IRQHandler	      ;  135: EPORT7_3
                DCD     EPORT7_4_IRQHandler	      ;  136: EPORT7_4
                DCD     EPORT7_5_IRQHandler	      ;  137: EPORT7_5
                DCD     EPORT7_6_IRQHandler	      ;  138: EPORT7_6
                DCD     EPORT7_7_IRQHandler	      ;  139: EPORT7_7

                
__Vectors_End

__Vectors_Size  EQU     __Vectors_End - __Vectors

                AREA    |.text|, CODE, READONLY


; Reset Handler
                
Reset_Handler   PROC
                EXPORT  Reset_Handler             [WEAK]   

                IMPORT  LVD_EN
                LDR     R0, =LVD_EN
                BLX     R0
                    
;set lvd level at 2.0V 
                MOV		r0,#0x40000000
                MOV		r1,#0x40004000
                STR		r0,[r1,#0x5c]
                LSLS	r0,r0,#1
                STR		r0,[r1,#0x5c]
                ASRS	r0,r0,#1
                STR		r0,[r1,#0x5c]
                MOV		r0,r1
                LDR		r0,[r0,#0x5c]
                ORR		r0,r0,#0x400000
                STR		r0,[r1,#0x5c]
                
                LDR		r0,=0x01001314
                MOV		r1,#0x40004000
                STR		r0,[r1,#0x54]
                
                MOV		r1,#0x40004000
                LDR		r0,[r1,#0x5c]
                BIC		r0,r0,#0xc0000000
                STR		r0,[r1,#0x5c]
				
;set lvd level at 2.0V end

;                IMPORT  IO_Latch_Clr
;                LDR     R0, =IO_Latch_Clr
;                BLX     R0

                IMPORT  SystemInit
                IMPORT  __main
                IMPORT	Sys_CacheInit

                MOV     r0,#0xe
                LDR     R1,=0x40005000
                STRH    r0,[r1,#0]       ;Close WDT
                MOV     r0,#0
                LDR     r1,=0x40006000
                STRH    r0,[r1,#0]       ;Close TC	
                
                LDR     R0, =SystemInit
                BLX     R0

                LDR     R0, =Sys_CacheInit
                BLX     R0

                LDR     R0, =__main
                BX      R0
                ENDP
					


; Dummy Exception Handlers (infinite loops which can be modified)

NMI_Handler     PROC
				EXPORT  NMI_Handler               [WEAK]
				B       .
				ENDP
HardFault_Handler\
                PROC
                EXPORT  HardFault_Handler         [WEAK]
                B       .
                ENDP
MemManage_Handler\
                PROC
                EXPORT  MemManage_Handler         [WEAK]
                B       .
                ENDP
BusFault_Handler\
                PROC
                EXPORT  BusFault_Handler          [WEAK]
                B       .
                ENDP
UsageFault_Handler\
                PROC
                EXPORT  UsageFault_Handler        [WEAK]
                B       .
                ENDP
SVC_Handler     PROC
                EXPORT  SVC_Handler               [WEAK]
                B       .
                ENDP
DebugMon_Handler\
                PROC
                EXPORT  DebugMon_Handler          [WEAK]
                B       .
                ENDP
PendSV_Handler  PROC
                EXPORT  PendSV_Handler            [WEAK]
                B       .
                ENDP
SysTick_Handler PROC
                EXPORT  SysTick_Handler           [WEAK]
                B       .
                ENDP

Default_Handler PROC

                EXPORT  OTP_IRQHandler            [WEAK]
                EXPORT  PMU_IRQHandler            [WEAK]
                EXPORT  TC_IRQHandler             [WEAK]
                EXPORT  PIT1_IRQHandler           [WEAK]
                EXPORT  PIT2_IRQHandler           [WEAK]
                EXPORT  EDMAC1_IRQHandler         [WEAK]
                EXPORT  DMA1_IRQHandler           [WEAK]
                EXPORT  DMA2_IRQHandler           [WEAK]
                EXPORT  TRNG_IRQHandler           [WEAK]
                EXPORT  SEC_PGD_LD_FD_IRQHandler  [WEAK]
                EXPORT  AES_IRQHandler            [WEAK]
                EXPORT  QADC_IRQHandler           [WEAK]										
                EXPORT  SPI1_IRQHandler           [WEAK]					
                EXPORT  SPI2_IRQHandler           [WEAK]					
                EXPORT  SPI3_IRQHandler           [WEAK]					
                EXPORT  SPIM1_IRQHandler          [WEAK]					
                EXPORT  SCI1_IRQHandler           [WEAK]
                EXPORT  SCI2_IRQHandler           [WEAK]					
                EXPORT  I2C1_IRQHandler           [WEAK]					
                EXPORT  PWM0_IRQHandler           [WEAK]					
                EXPORT  PWM1_IRQHandler           [WEAK]					
                EXPORT  PWM2_IRQHandler           [WEAK]					
                EXPORT  PWM3_IRQHandler           [WEAK]
                EXPORT  EPORT0_0_IRQHandler            [WEAK]
                EXPORT  EPORT0_1_IRQHandler            [WEAK]
                EXPORT  EPORT0_2_IRQHandler            [WEAK]
                EXPORT  EPORT0_3_IRQHandler            [WEAK]
                EXPORT  EPORT0_4_IRQHandler            [WEAK]
                EXPORT  EPORT0_5_IRQHandler            [WEAK]
                EXPORT  EPORT0_6_IRQHandler            [WEAK]
                EXPORT  EPORT0_7_IRQHandler            [WEAK]
                EXPORT  EPORT1_0_IRQHandler            [WEAK]
                EXPORT  EPORT1_1_IRQHandler            [WEAK]
                EXPORT  EPORT1_2_IRQHandler            [WEAK]
                EXPORT  EPORT1_3_IRQHandler            [WEAK]
                EXPORT  EPORT1_4_IRQHandler            [WEAK]
                EXPORT  EPORT1_5_IRQHandler            [WEAK]
                EXPORT  EPORT1_6_IRQHandler            [WEAK]
                EXPORT  EPORT1_7_IRQHandler            [WEAK]
                EXPORT  SCI3_IRQHandler                [WEAK]				                      							                      				         			     
                EXPORT  PWMT1_IRQHandler               [WEAK]         			
                EXPORT  PWMT2_IRQHandler               [WEAK]                   
                EXPORT  PWM4_IRQHandler                [WEAK]          
                EXPORT  PWM5_IRQHandler                [WEAK]            
                EXPORT  PWM6_IRQHandler                [WEAK]            
                EXPORT  PWM7_IRQHandler                [WEAK]                                    
                EXPORT  EPORT2_0_IRQHandler            [WEAK] 	      
                EXPORT  EPORT2_1_IRQHandler            [WEAK]	      				
                EXPORT  EPORT2_2_IRQHandler            [WEAK]	      
                EXPORT  EPORT2_3_IRQHandler            [WEAK]	      
                EXPORT  EPORT2_4_IRQHandler            [WEAK]	      
                EXPORT  EPORT2_5_IRQHandler            [WEAK]	      
                EXPORT  EPORT2_6_IRQHandler            [WEAK]	      
                EXPORT  EPORT2_7_IRQHandler            [WEAK]	            
                EXPORT  EPORT5_0_IRQHandler            [WEAK]	      
                EXPORT  EPORT5_1_IRQHandler            [WEAK]	      				
                EXPORT  EPORT5_2_IRQHandler            [WEAK]	      
                EXPORT  EPORT5_3_IRQHandler            [WEAK]	      
                EXPORT  EPORT5_4_IRQHandler            [WEAK]	      
                EXPORT  EPORT5_5_IRQHandler            [WEAK]	      
                EXPORT  EPORT5_6_IRQHandler            [WEAK]	      
                EXPORT  EPORT5_7_IRQHandler            [WEAK]	      				
                EXPORT  EPORT6_0_IRQHandler            [WEAK]	      
                EXPORT  EPORT6_1_IRQHandler            [WEAK]	      				
                EXPORT  EPORT6_2_IRQHandler            [WEAK]	      
                EXPORT  EPORT6_3_IRQHandler            [WEAK]	      
                EXPORT  EPORT6_4_IRQHandler            [WEAK]	      
                EXPORT  EPORT6_5_IRQHandler            [WEAK]	      
                EXPORT  EPORT6_6_IRQHandler            [WEAK]	      
                EXPORT  EPORT6_7_IRQHandler            [WEAK]	      
                EXPORT  EPORT7_0_IRQHandler            [WEAK]	      
                EXPORT  EPORT7_1_IRQHandler            [WEAK]	      				
                EXPORT  EPORT7_2_IRQHandler            [WEAK]	      
                EXPORT  EPORT7_3_IRQHandler            [WEAK]	      
                EXPORT  EPORT7_4_IRQHandler            [WEAK]	      
                EXPORT  EPORT7_5_IRQHandler            [WEAK]	      
                EXPORT  EPORT7_6_IRQHandler            [WEAK]	      
                EXPORT  EPORT7_7_IRQHandler            [WEAK]	      
       				

OTP_IRQHandler
PMU_IRQHandler
TC_IRQHandler
PIT1_IRQHandler
PIT2_IRQHandler
EDMAC1_IRQHandler
DMA1_IRQHandler
DMA2_IRQHandler
TRNG_IRQHandler
SEC_PGD_LD_FD_IRQHandler
AES_IRQHandler
QADC_IRQHandler
SPI1_IRQHandler
SPI2_IRQHandler
SPI3_IRQHandler
SPIM1_IRQHandler
SCI1_IRQHandler
SCI2_IRQHandler
I2C1_IRQHandler
PWM0_IRQHandler
PWM1_IRQHandler
PWM2_IRQHandler
PWM3_IRQHandler
EPORT0_0_IRQHandler
EPORT0_1_IRQHandler
EPORT0_2_IRQHandler
EPORT0_3_IRQHandler
EPORT0_4_IRQHandler
EPORT0_5_IRQHandler
EPORT0_6_IRQHandler
EPORT0_7_IRQHandler
EPORT1_0_IRQHandler
EPORT1_1_IRQHandler
EPORT1_2_IRQHandler
EPORT1_3_IRQHandler
EPORT1_4_IRQHandler
EPORT1_5_IRQHandler
EPORT1_6_IRQHandler
EPORT1_7_IRQHandler
SCI3_IRQHandler                
PWMT1_IRQHandler          
PWMT2_IRQHandler                   
PWM4_IRQHandler           
PWM5_IRQHandler           
PWM6_IRQHandler           
PWM7_IRQHandler                                 
EPORT2_0_IRQHandler	      
EPORT2_1_IRQHandler	      
EPORT2_2_IRQHandler	      
EPORT2_3_IRQHandler	      
EPORT2_4_IRQHandler	      
EPORT2_5_IRQHandler	      
EPORT2_6_IRQHandler	      
EPORT2_7_IRQHandler	          
EPORT5_0_IRQHandler	      
EPORT5_1_IRQHandler	      
EPORT5_2_IRQHandler	      
EPORT5_3_IRQHandler	      
EPORT5_4_IRQHandler	      
EPORT5_5_IRQHandler	      
EPORT5_6_IRQHandler	      
EPORT5_7_IRQHandler	      
EPORT6_0_IRQHandler	      
EPORT6_1_IRQHandler	      
EPORT6_2_IRQHandler	      
EPORT6_3_IRQHandler	      
EPORT6_4_IRQHandler	      
EPORT6_5_IRQHandler	      
EPORT6_6_IRQHandler	      
EPORT6_7_IRQHandler	      
EPORT7_0_IRQHandler	      
EPORT7_1_IRQHandler	      
EPORT7_2_IRQHandler	      
EPORT7_3_IRQHandler	      
EPORT7_4_IRQHandler	      
EPORT7_5_IRQHandler	      
EPORT7_6_IRQHandler	      
EPORT7_7_IRQHandler	      


                B       .

                ENDP


                ALIGN


; User Initial Stack & Heap

                IF      :DEF:__MICROLIB

                EXPORT  __initial_sp
                EXPORT  __heap_base
                EXPORT  __heap_limit

                ELSE

                IMPORT  __use_two_region_memory
                EXPORT  __user_initial_stackheap

__user_initial_stackheap PROC
                LDR     R0, =  Heap_Mem
                LDR     R1, =(Stack_Mem + Stack_Size)
                LDR     R2, = (Heap_Mem +  Heap_Size)
                LDR     R3, = Stack_Mem
                BX      LR
                ENDP

                ALIGN

                ENDIF


                END
					
